package com.cpkso.buss.backend.user.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cpkso.buss.backend.user.domain.Permission;
import com.cpkso.buss.backend.user.domain.Role;
import com.cpkso.buss.backend.user.service.PermissionService;
import com.cpkso.buss.backend.user.service.RoleService;
import com.cpkso.buss.backend.user.service.UserService;
import com.runze.base.web.BaseController;
import com.runze.base.web.Response;

/**
 * @author wrzhxy@qq.com
 * @date 2018年3月27日
 */
@RestController
@RequestMapping("/auth")
public class AuthController extends BaseController {
	/**
	 * 增加角色
	 */
	@RequiresPermissions("role:create")
	@PostMapping("/role")
	public Response addRole(@ModelAttribute Role role) {
		roleService.create(role);
		return success();
	}
	
	/**
	 * 分配角色
	 */
	@RequiresPermissions({"user:update", "role:query"})
	@PutMapping("/role/grant")
	public Response grant(@RequestParam Long userId, @RequestParam Long roleId) {
		userService.grant(userId, roleId);
		return success();
	}
	
	/**
	 * 撤销角色
	 */
	@RequiresPermissions({"user:update", "role:query"})
	@PutMapping("/role/revoke")
	public Response revoke(@RequestParam Long userId, @RequestParam Long roleId) {
		userService.revoke(userId, roleId);
		return success();
	}
	
	/**
	 * 分配权限
	 */
	@RequiresPermissions({"role:update", "permission:query"})
	@PutMapping("/permission/grant")
	public Response roleHasPermission(@RequestParam Long roleId, @RequestParam Long permissionId) {
		roleService.grant(roleId, permissionId);
		return success();
	}
	
	/**
	 * 增加权限
	 */
//	@RequiresPermissions("permission:create")
	@PostMapping("/permission")
	public Response addPermission(@ModelAttribute Permission permission) {
		permissionService.create(permission);
		return success();
	}
	
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private PermissionService permissionService;
}
